package com.ruoyi.pack.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.pack.mapper.PackWarehouseFenceMapper;
import com.ruoyi.pack.domain.PackWarehouseFence;
import com.ruoyi.pack.service.IPackWarehouseFenceService;

/**
 * 仓库电子围栏Service业务层处理
 *
 * @author ruoyi
 */
@Service
public class PackWarehouseFenceServiceImpl implements IPackWarehouseFenceService
{
    @Autowired
    private PackWarehouseFenceMapper packWarehouseFenceMapper;

    /**
     * 查询仓库电子围栏
     *
     * @param fenceId 仓库电子围栏主键
     * @return 仓库电子围栏
     */
    @Override
    public PackWarehouseFence selectPackWarehouseFenceByFenceId(Long fenceId)
    {
        return packWarehouseFenceMapper.selectPackWarehouseFenceByFenceId(fenceId);
    }

    /**
     * 查询仓库电子围栏列表
     *
     * @param packWarehouseFence 仓库电子围栏
     * @return 仓库电子围栏
     */
    @Override
    public List<PackWarehouseFence> selectPackWarehouseFenceList(PackWarehouseFence packWarehouseFence)
    {
        return packWarehouseFenceMapper.selectPackWarehouseFenceList(packWarehouseFence);
    }

    /**
     * 根据仓库ID查询电子围栏列表
     *
     * @param warehouseId 仓库ID
     * @return 仓库电子围栏集合
     */
    @Override
    public List<PackWarehouseFence> selectPackWarehouseFenceByWarehouseId(Long warehouseId)
    {
        return packWarehouseFenceMapper.selectPackWarehouseFenceByWarehouseId(warehouseId);
    }

    /**
     * 新增仓库电子围栏
     *
     * @param packWarehouseFence 仓库电子围栏
     * @return 结果
     */
    @Override
    public int insertPackWarehouseFence(PackWarehouseFence packWarehouseFence)
    {
        packWarehouseFence.setCreateTime(DateUtils.getNowDate());
        return packWarehouseFenceMapper.insertPackWarehouseFence(packWarehouseFence);
    }

    /**
     * 修改仓库电子围栏
     *
     * @param packWarehouseFence 仓库电子围栏
     * @return 结果
     */
    @Override
    public int updatePackWarehouseFence(PackWarehouseFence packWarehouseFence)
    {
        packWarehouseFence.setUpdateTime(DateUtils.getNowDate());
        return packWarehouseFenceMapper.updatePackWarehouseFence(packWarehouseFence);
    }

    /**
     * 批量删除仓库电子围栏
     *
     * @param fenceIds 需要删除的仓库电子围栏主键
     * @return 结果
     */
    @Override
    public int deletePackWarehouseFenceByFenceIds(Long[] fenceIds)
    {
        return packWarehouseFenceMapper.deletePackWarehouseFenceByFenceIds(fenceIds);
    }

    /**
     * 删除仓库电子围栏信息
     *
     * @param fenceId 仓库电子围栏主键
     * @return 结果
     */
    @Override
    public int deletePackWarehouseFenceByFenceId(Long fenceId)
    {
        return packWarehouseFenceMapper.deletePackWarehouseFenceByFenceId(fenceId);
    }

    /**
     * 设置仓库围栏（批量操作）
     *
     * @param warehouseId 仓库ID
     * @param fenceList 围栏点列表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int setWarehouseFence(Long warehouseId, List<PackWarehouseFence> fenceList)
    {
        // 先删除原有的围栏点
        packWarehouseFenceMapper.deletePackWarehouseFenceByWarehouseId(warehouseId);

        // 新增围栏点
        int result = 0;
        if (fenceList != null && !fenceList.isEmpty()) {
            for (PackWarehouseFence fence : fenceList) {
                fence.setWarehouseId(warehouseId);
                fence.setCreateTime(DateUtils.getNowDate());
                result += packWarehouseFenceMapper.insertPackWarehouseFence(fence);
            }
        }
        return result;
    }
}
